import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:untitled1/app/utils/utilDialog.dart';
import 'package:untitled1/app/widgets/base_page/BaseStatefulPage.dart';

import '../../../../../res/myColors.dart';
import '../../../../../utils/utilRouter.dart';
import '../../../../../utils/utilScreen.dart';
import '../../../../../widgets/MyContainer.dart';
import '../../../../../widgets/toast/MyToast.dart';
import '../../../../../bean/KVBean.dart';
import 'EmployerPage_ServiceDialogController.dart';

//服务时间
class EmployerPage_ServiceDialog extends BaseStatefulPage {
  final Function(List<KVBean> list)? onConfirm;

  EmployerPage_ServiceDialog({super.key, this.onConfirm});

  @override
  BaseStatefulPageState<BaseStatefulPage> createState() {
    return _EmployerPage_ServiceDialog();
  }
}

class _EmployerPage_ServiceDialog
    extends BaseStatefulPageState<EmployerPage_ServiceDialog> {
  final controller = Get.put(EmployerPage_ServiceDialogController());

  @override
  void initState() {
    super.initState();
    controller.list.value.add(KVBean(start: "开始时间", end: "结束时间"));
    WidgetsBinding.instance.addPostFrameCallback((_) {
      controller.list.refresh();
    });
  }

  @override
  Widget buildPage(BuildContext context) {
    return Obx(
      () => Container(
        padding: const EdgeInsets.only(top: 10, left: 15, right: 15),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            Align(
              alignment: Alignment(0, -1),
              child:  Container(
                padding: EdgeInsets.only(bottom: 60),
                width: utilScreen.getScreenWidth(context),
                margin: EdgeInsets.only(top: 40),
                child: ListView.builder(
                  itemCount: controller.list.value.length,
                  // 总共显示 20 项
                  scrollDirection: Axis.vertical,
                  // 列表垂直滚动
                  reverse: false,
                  // 是否倒序，false 表示从上往下滚动
                  controller: ScrollController(),
                  // 控制滚动位置（非必须）
                  primary: false,
                  // 如果为 true，不允许同时设置 controller
                  physics: const BouncingScrollPhysics(),
                  // iOS 回弹效果
                  shrinkWrap: false,
                  // false 表示撑满父容器；嵌套场景设为 true
                  // padding: const EdgeInsets.all(10),
                  // 整体内边距
                  keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
                  // 滚动时关闭键盘
                  itemBuilder: (context, index) {
                    return getItemView(index);
                  },
                ),
              ),
            ),
            Align(
              alignment: Alignment(0, 0),
              child: Stack(
                children: [
                  Align(
                    alignment: Alignment.topCenter,
                    child: Text(
                      '服务时间',
                      style: TextStyle(
                        color: myColors.c_191919,
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.topRight,
                    child: GestureDetector(
                      child: Text('关闭'),
                      onTap: () {
                        utilRouter.back();
                      },
                    ),
                  ),
                ],
              ),
            ),
            Align(
              alignment: Alignment(0, 1),
              child: Container(
                width: utilScreen.getScreenWidth(context) - 50,
                height: 50,
                margin: EdgeInsets.only(bottom: 20),
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(50),
                ),
                child: Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () {
                          controller.list.clear();
                          controller.list.refresh();
                          controller.list.value.add(KVBean(start: "开始时间", end: "结束时间"));
                        },
                        child: Container(
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: myColors.c_F89596, // 背景色
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(50), // 左上圆角
                              bottomLeft: Radius.circular(50), // 左下圆角
                              topRight: Radius.zero, // 右上直角
                              bottomRight: Radius.zero, // 右下直角
                            ),
                          ),
                          child: Text(
                            '重置',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () {
                          // print(">]aaaaaa");

                          utilRouter.back();
                        },
                        child: Container(
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: myColors.c_FF4142, // 背景色
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.zero, // 左上圆角
                              bottomLeft: Radius.zero, // 左下圆角
                              topRight: Radius.circular(50), // 右上直角
                              bottomRight: Radius.circular(50), // 右下直角
                            ),
                          ),
                          child: Text(
                            '确定',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget getItemView(int index) {
    KVBean item =controller.list.value[index];
    String title="";
    if (index==controller.list.length-1) {
      title = "添加时间段";
      item.type=-1;
    }else{
      title = "删除时间段";
      item.type=1;
    }
    return Container(
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('第${index+1}时间段',style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold,color: myColors.c_191919),),
              GestureDetector(
                onTap: () {
                if (item.type==-1) {
                  controller.list.value.add(KVBean(start: "开始时间", end: "结束时间"));
                }else if(item.type==1){
                  controller.list.value.removeAt(index);
                }
                  controller.list.refresh();
                },
                child: Text(title),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
             TextButton(onPressed: (){
               utilDialog.showTimeRangePicker(context,8,12,(startTime,endTime){
                    print(">] startTime=${startTime},end=${endTime}");
                    item.start=startTime.toString();
                    item.end=endTime.toString();
                    controller.list.refresh();
               });
               }, child: Text("${item.start}")
             ) ,
              Text('-'), 
             TextButton(onPressed: (){}, child: Text("${item.end}"))

            ]
          )
        ],
      ),
    );
  }




}

